package basic.study.leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName Hard128
 * @Description 最长连续序列
 * @Company inspur
 * @Author Kevin
 * @Date 2020/6/29 11:03
 * @Version 1.0
 */
public class Hard128 {
    class Solution {
        public int longestConsecutive(int[] nums) {
            Set<Integer> num_set = new HashSet<>();
            for (int num: nums) {
                num_set.add(num);
            }

            int longestStreak = 0;
            for (int num: nums) {
                if (!num_set.contains(num -1 )) {
                    int curNum = num;
                    int currentStreak = 1;

                    while (num_set.contains(curNum+1)) {
                        currentStreak ++;
                        curNum ++;
                    }
                    longestStreak = Math.max(longestStreak, currentStreak);
                }
            }
            return longestStreak;
        }
    }
}
